#!/usr/bin/python2.5
#
# data_object.py
#
# Copyright 2012 Maxim Velikobratov (Jaropolk)
#
# Licensed under GNU GPL v3;
#
#     http://www.gnu.org/licenses/gpl.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Datastore models."""
from google.appengine.ext import db

import utility

class Recipes(db.Model):
  author = db.UserProperty(required=True)
  name = db.StringProperty(required=True)
  pub_date = db.DateTimeProperty(auto_now_add=True)
  descript = db.StringProperty(multiline=True)
  is_deleted = db.BooleanProperty(default=False)
  is_publiced = db.BooleanProperty(default=False)
  
  def __str__(self):
    """Overridden string representation."""
    return self.name

  def put(self):
    """Saves the recipe and flushes the memcache."""
    super(Recipes, self).put()
    utility.clear_memcache()

  def delete(self):
    """Overridden to ensure memcache is cleared."""
    super(Recipes, self).delete()
    utility.clear_memcache()

  @staticmethod
  def getByName(name):
    key = 'recipe-name:' + name
    recipe = utility.memcache_get(key)
    if not recipe:
      recipe = Recipes.all().filter("name = ", name).get()
      utility.memcache_set(key, recipe)

    return  recipe

# class IngredientSubclasses(db.Model):
#   name = db.StringProperty(required=True)
#   subclass = db.SelfReferenceProperty(IngredientSubclasses)

# class Ingredients(db.Model):
#   name = db.StringProperty(required=True)
#   subclass = db.StringProperty(required=True)
# ReferenceProperty(IngredientSubclasses,
#    collection_name="ingredientsubclasses_ref_one_set")

class UserProfiles(db.Model):
  """A class that represents the access levels of a given user."""
  user = db.UserProperty(required=True)
  email = db.EmailProperty(required=True)
  reg_date = db.DateTimeProperty(auto_now_add=True)
  name = db.StringProperty()
  midname = db.StringProperty()
  surname  = db.StringProperty()
  birthday = db.StringProperty()
  gender = db.StringProperty()
  city = db.StringProperty()
  is_deleted = db.BooleanProperty(default=False)

  def __str__(self):
    """Overridden string representation."""
    return self.name + " " + self.surname

  @staticmethod
  def load(email):
    """Retrieves a given user profile, through memcache if possible.

    Args:
      email: email address of the profile to load

    Returns:
      UserProfile object for the given email address

    """
    key = 'email:' + email
    profile = utility.memcache_get(key)
    if not profile:
      profile = UserProfiles.all().filter('email =', email).get()
      utility.memcache_set(key, profile)
    return profile

  def put(self):
    """Saves the profile and flushes the memcache."""
    super(UserProfiles, self).put()
    utility.clear_memcache()
  
  def delete(self):
    """Overridden to ensure memcache is cleared."""
    super(UserProfile, self).delete()
    utility.clear_memcache()

